package com.netposa.datawarehouse.bodysearch.service.impl;

import com.netposa.datawarehouse.bodysearch.service.RequestIdService;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RequestIdServiceImpl implements RequestIdService {
   @Autowired
   private RedisTemplate redisTemplate;

   public RequestAssociateValue get(String requestId) {
      RequestAssociateValue value = (RequestAssociateValue)this.redisTemplate.boundHashOps(this.bodyInfoKey() + requestId).get(requestId);
      return value;
   }

   public void set(String requestId, RequestAssociateValue value) {
      this.redisTemplate.opsForHash().put(this.bodyInfoKey() + requestId, requestId, value);
      this.redisTemplate.expire(this.bodyInfoKey() + requestId, 10L, TimeUnit.MINUTES);
   }

   public String generateRequestId() {
      return UUID.randomUUID().toString().replace("-", "");
   }

   public void set(RequestAssociateValue value) {
      value.setRequestId(this.generateRequestId());
      this.set(value.getRequestId(), value);
   }

   private String bodyInfoKey() {
      return "datawarehouse:bodyTemp:";
   }
}
